package com.hbsd.paopaobackend.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbsd.paopaobackend.common.BaseResponse;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.common.ResultUtils;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.Comment.AddCommentRequest;
import com.hbsd.paopaobackend.model.request.Comment.CommentPageRequest;
import com.hbsd.paopaobackend.model.vo.Comment.CommentPageVO;
import com.hbsd.paopaobackend.service.CommentService;
import com.hbsd.paopaobackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "评论模块")
@RestController
@RequestMapping("/comment")
// 配置跨域 allowCredentials: 允许携带cookie
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")

public class CommentController {
    
    @Resource
    private UserService userService;

    @Resource
    private CommentService commentService;



    /**
     * 添加博客评论
     * @param addCommentRequest
     * @param request
     * @return
     */
    @ApiOperation(value="添加博客评论", notes="添加博客评论 addComment")
//    AddCommentRequest
    @PostMapping("/add")
    public BaseResponse<Boolean> addComment(@RequestBody AddCommentRequest addCommentRequest, HttpServletRequest request) {
        if(addCommentRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "评论参数不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Boolean result = commentService.addComment(addCommentRequest, loginUser);

        return ResultUtils.success(result);
    }

    /**
     * 分页查询博客评论
     * @param commentPageRequest
     * @param request
     * @return
     */
    @ApiOperation(value="分页查询博客评论", notes="分页查询博客评论 listCommentPage")
    @PostMapping("/list/page")
    public BaseResponse<Page<CommentPageVO>> listCommentPage(@RequestBody CommentPageRequest commentPageRequest, HttpServletRequest request) {
        if(commentPageRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分页参数不能为空");
        }

        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN, "用户未登录");
        }

        Page<CommentPageVO> page = commentService.listCommentPage(commentPageRequest, loginUser);

        return ResultUtils.success(page);
    }

}
